package com.frcdi.qms.masterdata.service.impl;

import com.frcdi.qms.common.constant.UserConstants;
import com.frcdi.qms.common.utils.DateUtils;
import com.frcdi.qms.common.utils.SecurityUtils;
import com.frcdi.qms.common.utils.StringUtils;
import com.frcdi.qms.masterdata.domain.Change;
import com.frcdi.qms.masterdata.domain.CodeConfig;
import com.frcdi.qms.masterdata.domain.File;
import com.frcdi.qms.masterdata.mapper.ChangeMapper;
import com.frcdi.qms.masterdata.mapper.CodeConfigMapper;
import com.frcdi.qms.masterdata.mapper.FileMapper;
import com.frcdi.qms.masterdata.service.IChangeService;
import com.frcdi.qms.system.service.ISysConfigService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 变更申请Service业务层处理
 *
 * @author cell
 * @date 2023-09-25
 */
@Service
public class ChangeServiceImpl implements IChangeService
{
    @Autowired
    private ChangeMapper changeMapper;

    @Autowired
    private FileMapper fileMapper;

    @Autowired
    private ISysConfigService configService;

    @Autowired
    private CodeConfigMapper codeMapper;

    /**
     * 查询变更申请
     *
     * @param changeId 变更申请主键
     * @return 变更申请
     */
    @Override
    public Change selectChangeByChangeId(Long changeId)
    {
        return changeMapper.selectChangeByChangeId(changeId);
    }

    @Override
    public Change selectChangeByChangeNo(String changeNo)
    {
        return changeMapper.selectChangeByChangeNo(changeNo);
    }

    /**
     * 查询变更申请
     *
     * @param changeNo 变更申请编号
     * @return 变更申请
     */
    @Override
    public Boolean checkChangeNoUnique(String changeNo)
    {
        if (StringUtils.isNotNull(changeMapper.checkChangeNoUnique(changeNo))) {
            return UserConstants.NOT_UNIQUE;
        }
        return UserConstants.UNIQUE;
    }

    /**
     * 查询变更申请列表
     *
     * @param change 变更申请
     * @return 变更申请
     */
    @Override
    public List<Change> selectChangeList(Change change)
    {
        return changeMapper.selectChangeList(change);
    }

    /**
     * 新增变更申请
     *
     * @param change 变更申请
     * @return 结果
     */
    @Transactional
    @Override
    public String insertChange(Change change)
    {
        int row = changeMapper.insertChange(change);
        if (row > 0) {
            List<File> fileList = change.getFileList();
            if (fileList != null && fileList.size() > 0) {
                for (File file : fileList) {
                    file.setTableNo(change.getChangeNo());
                    file.setCreateBy(SecurityUtils.getLoginUser().getUsername());
                    fileMapper.insertFile(file);
                }
            }
        }
        return change.getChangeId().toString();
    }

    /**
     * 修改变更申请
     *
     * @param change 变更申请
     * @return 结果
     */
    @Transactional
    @Override
    public int updateChange(Change change)
    {
        int row = changeMapper.updateChange(change);
        if (row > 0) {
            //  先将已有的删除 再重新插入新的文件数据
            List<File> fileList = change.getFileList();
            if (fileList != null && fileList.size() > 0) {
                fileMapper.deleteFileByTableNo(change.getChangeNo());
                for (File file : fileList) {
                    file.setTableNo(change.getChangeNo());
                    file.setCreateBy(SecurityUtils.getLoginUser().getUsername());
                    fileMapper.insertFile(file);
                }
            }
        }
        return row;
    }

    @Transactional
    @Override
    public int updateChangeByChangeNo(Change change)
    {
        int row = changeMapper.updateChangeByChangeNo(change);
        return row;
    }

    /**
     * 批量删除变更申请
     *
     * @param changeIds 需要删除的变更申请主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteChangeByChangeIds(Long[] changeIds)
    {
        for (Long changeId : changeIds) {
            String changeNo = selectChangeByChangeId(changeId).getChangeNo();
            fileMapper.deleteFileByTableNo(changeNo);
        }
        int row = changeMapper.deleteChangeByChangeIds(changeIds);
        return row;
    }

    /**
     * 删除变更申请信息
     *
     * @param changeId 变更申请主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteChangeByChangeId(Long changeId) {
        String changeNo = selectChangeByChangeId(changeId).getChangeNo();
        fileMapper.deleteFileByTableNo(changeNo);
        int row = changeMapper.deleteChangeByChangeId(changeId);
        return row;
    }

    @Override
    public String getCode() {
        String syscode = new StringBuilder().toString();
        // 获取前缀
        CodeConfig codeConfig = codeMapper.selectCodeConfigByCodeId(1L);
        if (StringUtils.equals(UserConstants.CODE_ADDTIME,codeConfig.getCodeTime())){
            syscode = codeConfig.getCodeRule()+DateUtils.dateTime();
        }else {
            syscode = codeConfig.getCodeRule();
        }
        // 获取最近一次生成
        String code = changeMapper.getCode(syscode);

        if (StringUtils.isNull(code)){
            return syscode + StringUtils.padl(1,codeConfig.getCodeNumber());
        }

        String newcode = StringUtils.substring(code, code.length() - codeConfig.getCodeNumber(), code.length());
        return syscode + (StringUtils.padl(Integer.valueOf(newcode)+1,codeConfig.getCodeNumber()));
    }
}
